package com.dc.free.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLDecoder;

/**
 * 输入输出流处理
 * @author linquan
 *
 */
public class IOUtil {

	private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
	
	/**
	 * 关闭流
	 * @param closeable
	 */
	public static void close(Closeable closeable) {
		try {
			if (closeable != null) {
				closeable.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * InputStream 转成 File
	 * @param input 
	 * @param file 
	 * @throws IOException
	 */
	public static void toFile(InputStream input, File file) throws IOException {
		OutputStream os = new FileOutputStream(file);
		int bytesRead = 0;
		byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
		while ((bytesRead = input.read(buffer, 0, DEFAULT_BUFFER_SIZE)) != -1) {
			os.write(buffer, 0, bytesRead);
		}
		IOUtil.close(os);
		IOUtil.close(input);
	}
	
	/**
	 * 写入
	 * @param is
	 * @param os
	 * @return
	 * @throws IOException
	 */
	public static long write(InputStream is, OutputStream os) throws IOException {
	    long size=0;
	    int read;
	    byte[] buf = new byte[DEFAULT_BUFFER_SIZE];
	    
	    while((read = is.read(buf)) != -1) {
	      os.write(buf, 0, read);
	      size += read;
	    }
	    return size;
	}
	
	/**
	 * 读取数据
	 * @param is
	 * @return
	 * @throws IOException
	 */
	public static String read(InputStream is) throws IOException {
		String result = null;
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		write(is, baos);
		result = baos.toString();
		IOUtil.close(baos);
		return result;
	}
	
	public static InputStream getInputStream(byte[] bt) {
		return new ByteArrayInputStream(bt);
	}
	
	public static InputStream getInputStream(String s) {
		return getInputStream(s.getBytes());
	}
	
	public static long write(String s, OutputStream os) throws IOException {
		ByteArrayInputStream is = new ByteArrayInputStream(s.getBytes());
	    return write(is, os);
	}
	
	/**
	 * 读取资源流
	 * @param resource
	 * @return
	 * @throws IOException
	 */
	public static InputStream readResource(String resource) throws IOException {
		InputStream is = null;
		is = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
		if (is == null) {
			is = IOUtil.class.getResourceAsStream(resource);
		}

		return is;
	}
	
	/**
	 * 读取资源字符串
	 * @param resource
	 * @return
	 */
	public static String readWithString(String resource) {
		try {
			InputStream is = readResource(resource);
			String result = read(is);
			IOUtil.close(is);
			return result;
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	/**
	 * 获取绝对路径
	 * @param path
	 * @return
	 */
	public static String getCanonicalPath(String path) {
		try {
			return URLDecoder.decode(new File(path).getCanonicalPath(), "utf-8");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}
